package com.ybear.ybcomponent.widget.linkage.view

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import com.ybear.ybcomponent.widget.damping.DampingNestedScrollView
import com.ybear.ybcomponent.widget.linkage.interfaces.ILinkage
import com.ybear.ybcomponent.widget.linkage.LinkageAnimationHelper
import com.ybear.ybcomponent.widget.linkage.interfaces.OnScrollListener

/**
 * @Author MiWi
 * @Date 2025年2月13日
 * @Description Linkage - 联动Nested阻尼滑动控件
 *
 * This custom nested scrolling control provides a set of configurable scrolling effects,
 * which can be applied to child views via XML attributes.
 *
 * 功能说明：
 * 需要配合 [LinkageAppBar] 一起使用。
 * 关于属性参数，[LinkageAppBar] 注释中有声明
 */
open class LinkageDampingNestedScrollView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : DampingNestedScrollView(context, attrs, defStyleAttr), ILinkage {

    private val helper = LinkageAnimationHelper()

    override fun onFinishInflate() {
        super.onFinishInflate()
        helper.onFinishInflate( this )
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(ev: MotionEvent): Boolean {
        helper.onTouchEvent( this , ev )
        return super.onTouchEvent( ev )
    }

    override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
        super.onScrollChanged(l, t, oldl, oldt)
        helper.onScrollChanged( this, l, t, oldl, oldt )
    }

    override fun getHelper(): LinkageAnimationHelper { return helper }

    override fun addOnTouchListener(listener: OnTouchListener) {
        helper.addOnTouchListener( listener )
    }

    override fun removeOnTouchListener(listener: OnTouchListener) {
        helper.removeOnTouchListener( listener )
    }

    override fun addOnScrollListener(listener: OnScrollListener) {
        helper.addOnScrollListener( listener )
    }

    override fun removeOnScrollListener(listener: OnScrollListener) {
        helper.removeOnScrollListener( listener )
    }
}